package cn.ffcs.businessrequest.web;


import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.NewprodReq;
import cn.ffcs.businessrequest.model.ReqDocu;
import cn.ffcs.businessrequest.service.INewprodReqService;
import cn.ffcs.businessrequest.service.IReqDocuService;
import cn.ffcs.common.model.Attach;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.IOrgRelaPersonService;
import cn.ffcs.util.context.ApplicationContextUtil;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.system.dict.model.DictType;
import cn.ffcs.system.dict.service.DictService;
import cn.ffcs.system.dict.service.DictTypeService;

/**
 * 
 * 新产品需求action层，实现页面业务逻辑<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class NewprodReqAction<P extends NewprodReq> extends BaseAction<P>
{
    private static final int BUFFER_SIZE = 512 * 1024;
    /** 业务POJO **/
    private P newprodReq;
    /** 附件上传 **/
    private List<File> uploads;
    /** 上传附件名称 **/
    private List<String> fileNames;
    /** 新产品需求服务 **/
    private INewprodReqService newprodReqService;
    /** 文档服务 **/
    private IReqDocuService reqDocuService;
    /** 上传附件名称 **/
    private IOrgRelaPersonService orgRelaPersonService;
    /** 需求文档 **/
    private List<ReqDocu> reqDocus;
    /** 组织人员关系对象 **/
    private OrgRelaPerson orgRelaPerson;
    /** 需求类型 **/
    private List<Dict> types;
    /** 重要性 **/
    private List<Dict> importances;

    /** 数据字典类型服务 **/
    private DictTypeService dictTypeService;
    /** 数据字典服务 **/
    private DictService dictService;
    
    
    /**
     * 
     * 初始化字典数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public void initDict(){
        String reqNewProdtypes = "REQ_NEW_PROD_TYPE";
        String reqImportType = "REQ_IMPORT_TYPE";
        Criterion cond = Restrictions.eq("keyValue", reqImportType );
        Criterion cond1 = Restrictions.eq("keyValue",  reqNewProdtypes );
        List dictType = dictTypeService.findBy(cond);
        List dictType1 = dictTypeService.findBy(cond1);
        //重要性
        if (dictType != null && dictType.size() > 0)
        {
            for (int i = 0; i < dictType.size(); i++)
            {
                Criterion dtcond = Restrictions.eq("dictType.id",
                        ((DictType) dictType.get(i)).getId());
                    importances = (List<Dict>) dictService.findBy(dtcond);
            }
        }
        //需求类型
        if (dictType1 != null && dictType1.size() > 0)
        {
            for (int i = 0; i < dictType1.size(); i++)
            {
                Criterion dtcond = Restrictions.eq("dictType.id",
                        ((DictType) dictType1.get(i)).getId());
                types = (List<Dict>) dictService.findBy(dtcond);
            }
        }
    }
    
    /**
     * 初始化业务数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String init(){
        if (newprodReq!=null){
            Criterion cond1 = Restrictions.eq("refEntity", newprodReq.getClass().getName());
            Criterion cond2 = Restrictions.eq("refEntityId", newprodReq.getId().toString());
            Criterion condAttach = Restrictions.and(cond1, cond2);
            reqDocus=reqDocuService.findBy(condAttach);
        }
        initDict();
        return SUCCESS;
    }
    
    public Organization getOrgByPerson(Person person ){
        if (newprodReq!=null&&person!=null&&person.getId()>0){
            return orgRelaPersonService.findByPageByPersonId(person.getId()+"");
        }
        return null;
    }
    
    /**
     * 保存操作：
     * 保存业务和流程数据信息
     * @return
     */
    public String saveOrUpdate()
    {
        // 获取附件是否为发布
        String[] attachIDs = getContextObjects("attachIDs");
        // 获取附件类型
        String [] fileTypes = getContextObjects("filetype");
        // 删除附件
        String[] delIDs = getContextObjects("delIDs");
        
        // 保存产品资料数据
        if(newprodReq!=null){
            if(newprodReq.getReqCode()==null||"".equals(newprodReq.getReqCode())){
                newprodReq.setReqCode(getReqCode());
            }
            if(newprodReq.getStartPerson()!=null&&newprodReq.getStartPerson().getId()!=null){
                newprodReq.setStartDept(getOrgByPerson(newprodReq.getStartPerson()));
            }else{
                newprodReq.setStartPerson(null);
                newprodReq.setStartDept(null);
            }
            if(newprodReq.getProdPerson()!=null&&newprodReq.getProdPerson().getId()!=null){
                newprodReq.setProdDept(getOrgByPerson(newprodReq.getProdPerson()));
            }else{
                newprodReq.setProdPerson(null);
                newprodReq.setProdDept(null);
            }
            
            newprodReqService.saveNewprodReq(newprodReq,uploads, fileNames, fileTypes, delIDs);
        }

        //初始化数据
        findById();
        //流程初始化
        return SUCCESS;
    }
    
    public String findById()
    {
        if (newprodReq != null && newprodReq.getId() != 0)
        {
            newprodReq = (P) baseService.findById(newprodReq.getId());
        }
        //业务数据初始化
        init();
        return SUCCESS;
    }
    
    /**
     * 删除操作
     */
    public String remove()
    {
        baseService.remove(newprodReq);
        return SUCCESS;
    }
    

    /**
     * 获取需求编号操作
     * @return
     */
    public String getReqCode()
    {
        String nextval= baseService.getNextval("seq_req_num");
        return "FJ"+nextval;
    }
    
    
    public List<File> getUpload()
    {
        return uploads;
    }

    public void setUpload(List<File> uploads)
    {
        this.uploads = uploads;
    }
    
    public List<String> getUploadFileName()
    {
        return fileNames;
    }

    public void setUploadFileName(List<String> fileNames)
    {
        this.fileNames = fileNames;
    }

    public INewprodReqService getNewprodReqService()
    {
        return newprodReqService;
    }

    public void setNewprodReqService(INewprodReqService newprodReqService)
    {
        this.newprodReqService = newprodReqService;
    }

    public List<ReqDocu> getReqDocus()
    {
        return reqDocus;
    }

    public void setReqDocus(List<ReqDocu> reqDocus)
    {
        this.reqDocus = reqDocus;
    }

    public IOrgRelaPersonService getOrgRelaPersonService()
    {
        return orgRelaPersonService;
    }

    public void setOrgRelaPersonService(IOrgRelaPersonService orgRelaPersonService)
    {
        this.orgRelaPersonService = orgRelaPersonService;
    }

    public OrgRelaPerson getOrgRelaPerson()
    {
        return orgRelaPerson;
    }

    public void setOrgRelaPerson(OrgRelaPerson orgRelaPerson)
    {
        this.orgRelaPerson = orgRelaPerson;
    }

    public P getNewprodReq()
    {
        return newprodReq;
    }

    public void setNewprodReq(P newprodReq)
    {
        this.newprodReq = newprodReq;
    }

    public IReqDocuService getReqDocuService()
    {
        return reqDocuService;
    }

    public void setReqDocuService(IReqDocuService reqDocuService)
    {
        this.reqDocuService = reqDocuService;
    }

    public List<Dict> getTypes()
    {
        return types;
    }

    public void setTypes(List<Dict> types)
    {
        this.types = types;
    }

    public List<Dict> getImportances()
    {
        return importances;
    }

    public void setImportances(List<Dict> importances)
    {
        this.importances = importances;
    }

    public DictTypeService getDictTypeService()
    {
        return dictTypeService;
    }

    public void setDictTypeService(DictTypeService dictTypeService)
    {
        this.dictTypeService = dictTypeService;
    }

    public DictService getDictService()
    {
        return dictService;
    }

    public void setDictService(DictService dictService)
    {
        this.dictService = dictService;
    }



}
